

const generateMenuTree = (list) => {
    //先把所有的菜单放在一个map里，key是菜单的ID，值是菜单的浅克隆出来的对象
    const map = list.reduce((prev, current) => {
        const temp = { ...current };
        prev[current.id] = temp;
        return prev;
    }, {});
    const tree = [];
    list.forEach((item) => {
        const temp = map[item.id];//先找到此ID对应的对象
        const parent_id = temp.parent_id;//再找到此对象对应的父ID
        if (parent_id && map[parent_id]) {//如果有父节点，则把此节点添到父节点的children数组中
            const parent = map[parent_id];
            parent.children = parent.children || [];
            parent.children.push(temp);
        } else {//如果没有父节点，则直接添加成一级节点
            tree.push(temp);
        }
    });
    return tree;
}
export default generateMenuTree;